package com.xinfu.newproject.model.pay_cost;

import com.xinfu.newproject.apiservice.JsNetworkService;
import com.xinfu.newproject.bean.pay.body.PayRecordsBody;
import com.xinfu.newproject.bean.paycost.bean.GetBillByTypeBean;
import com.xinfu.newproject.bean.paycost.bean.QueryTypesBean;
import com.xinfu.newproject.bean.paycost.body.GetBillByTypeBody;
import com.xinfu.newproject.bean.paycost.body.QueryTypesBody;
import com.xinfu.newproject.callback.MyCallBack;
import com.xinfu.newproject.model.IModel;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class PayCostModel implements IModel.IPayCostModel {

    @Override
    public void queryTypes(QueryTypesBody queryTypesBody, MyCallBack<QueryTypesBean> callBack) {
//        Map<String, Object> map = new HashMap<>();
        JsNetworkService.getInstance3().getIJsNetworkService()
                .queryTypes(queryTypesBody.getHeader())
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<QueryTypesBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Disposable mDisposable; //用于Activity销毁时停止执行
                        mDisposable = d;
                        callBack.onDispose(mDisposable);
                    }

                    @Override
                    public void onNext(@NonNull QueryTypesBean queryTypesBean) {

                        callBack.onSuccess(queryTypesBean);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        callBack.onError("Server Error", "网络异常，请稍后再试");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void getBillsByType(GetBillByTypeBody getBillByTypeBody, MyCallBack<GetBillByTypeBean> callBack) {
        Map<String, Object> map = new HashMap<>();
        map.put("roomId", getBillByTypeBody.getRoomId());
        map.put("type", getBillByTypeBody.getType());
        map.put("cusOrRent", getBillByTypeBody.getCusOrRent());
        JsNetworkService.getInstance3().getIJsNetworkService()
                .getBillsByType(getBillByTypeBody.getHeader(), map)
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<GetBillByTypeBean>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Disposable mDisposable; //用于Activity销毁时停止执行
                        mDisposable = d;
                        callBack.onDispose(mDisposable);
                    }

                    @Override
                    public void onNext(@NonNull GetBillByTypeBean getBillByTypeBean) {

                        callBack.onSuccess(getBillByTypeBean);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        callBack.onError("Server Error", "网络异常，请稍后再试");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void payRecords(PayRecordsBody payRecordsBody, MyCallBack<String> callBack) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", payRecordsBody.getUserId());

        JsNetworkService.getInstance3().getIJsNetworkService()
                .getUserPayRecords(payRecordsBody.getHeader(), payRecordsBody.getUserId())
                .throttleFirst(1, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        Disposable mDisposable; //用于Activity销毁时停止执行
                        mDisposable = d;
                        callBack.onDispose(mDisposable);
                    }

                    @Override
                    public void onNext(@NonNull String payRecordsBean) {

                        callBack.onSuccess(payRecordsBean);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        callBack.onError("Server Error", "网络异常，请稍后再试");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }


}
